local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local TweenHelper = _G.CommonOHOH.load.TweenHelper();
local BtnHelper = ClientOHOH.load.BtnHelper();

local CarTool=_G.CommonOHOH.load.CarTool();
local cellPerfab=ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.PartCell")

local pInfo=ClientOHOH.load.UIManager():CreateUI("PartInfo",require(script.PartInfo))

function InitInsts(ui)
	ui.insts = {
        cell = "ctrl.cell",
        btn = "ctrl.btn",
        viewportFrame = "ctrl.ViewportFrame", --展示模型
        partImage = "ctrl.partImage",
        rollTitle = "ctrl.rollTitle", --roll的title 格式： 1 in 2000
        partNum = "ctrl.partNum", --数量 格式：x1
        partIn = "ctrl.partIn", --品阶 格式：2 in
        partWeight = "ctrl.partWeight", --weight 格式：2000
        selectedIcon = "ctrl.selectedIcon",
        hadIcon = "ctrl.hadIcon",
        BackColor={
            [1]="ctrl.back.Normal",
            [2]="ctrl.back.Shiny",
            [3]="ctrl.back.Rainbow",
            [4]="ctrl.back.Void",
            [5]="ctrl.back.Astral",
        }
    };

end;

function M.new(...)
	return M(...);
end;


--参数：
--carId,汽车id
--guid:cell的唯一Id(可不传)
--parent:cell所在父节点
--dataBase：玩家数据库

function M.ctor(self,carId,parent,database,guid)
    InitInsts(self)
    M.super.ctor(self,cellPerfab:Clone())
    self:SetParent(parent)
    self.main_inst.Visible=true
    self.carId=carId
    self.guid=guid
    self.DataBase=database
    self.data=CarTool.GetPartAllInfo(self.carId)
    self:init()
end;

--默认只有汽车图片，背景（品阶）显示,数量需要调用SetPartNum函数设置值
function M.init(self)
    self.insts.partImage.Image = CarTool.GetPartImage(self.carId)

    local partId,grade = CarTool.GetPartIdAndGrade(self.carId)
    self.insts.rollTitle.Text = "1 in "..self.data.cfg.weight

    self.insts.partWeight.Text = self.data.cfg.weight
    self.insts.partIn.Text = "1 in"
    --默认x1，修改调用函数SetPartNum
    self.insts.partNum.Text = "x1"
    for i = 1, 5, 1 do
        if i == grade then
            self.insts.BackColor[i].Visible = true
        else
            self.insts.BackColor[i].Visible = false
        end
    end

    self.insts.rollTitle.Visible = false
    self.insts.partNum.Visible = false
    self.insts.partIn.Visible = false
    self.insts.partWeight.Visible = false
    self.insts.selectedIcon.Visible = false
    self.insts.hadIcon.Visible = false

end

function M.dtor(self)
    --self:HideTip()
    self.main_inst:Destroy()
	M.super.dtor(self);
end;

--********************************************************************************

--以下是接口

-------------各种标记----锁定，准备，选择，删除（售卖）------------------------------------------------
--显示tip
function M.ShowTip(self)
    pInfo:OnShow(self)
end
--隐藏tip
function M.HideTip(self)
    pInfo:OnHide()
end

--设置显示状态（隐藏，或者显示）
function M.SetShowStatus(self,isShow)
    self.main_inst.Visible=isShow
end

--设置选中状态(true,会出现红心)
function M.SetSelectedStatus(self,isSelelct)
    self.insts.selectedIcon.Visible=isSelelct
end

--设置拥有状态
function M.SetHadStatus(self,isHad)
    self.insts.hadIcon.Visible=isHad
end

--设置title的可视状态，roll的时候才show
function M.SetTitleVisibility(self,isVisible)
    self.insts.rollTitle.Visible = isVisible
end

--设置weight可视状态
function M.SetPartWeightVisibility(self,isVisible)
    self.insts.partWeight.Visible = isVisible
end

--设置品阶可视状态
function M.SetPartInVisibility(self,isVisible)
    self.insts.partIn.Visible = isVisible
end

--设置数量显示状态
function M.SetPartNumVisibility(self,isVisible)
    self.insts.partNum.Visible = isVisible
end

--设置数量文本 needNum不传显示xN,传则显示n/m
function M.SetPartNum(self,hadNum,needNum)
    if needNum then
        self.insts.partNum.Text = hadNum.."/"..needNum
        if tonumber(hadNum) >= tonumber(needNum) then
            self.insts.partNum.TextColor3 = Color3.new(0.219607, 0.713725, 0.278431)
        else
            self.insts.partNum.TextColor3 = Color3.new(0.713725, 0.250980, 0.219607)
        end
        return 
    end
    self.insts.partNum.Text = "x"..hadNum
end

--是否拥有
function M.isHad(self)
    return  self.insts.hadIcon.Visible
end

--是否被选中
function M.isSelected(self)
    return self.insts.selectedIcon.Visible
end

function M.GetParent(self)
    return self.main_inst.Parent
end

function M.SetParent(self,parent)
    self.main_inst.Parent=parent
end




----------------标签End----------------------------


-----------------汽车信息相关-----------------------------


--获取cfg
function M.GetPartCfgData(self)
    return self.data.cfg
end
--获取模型
function M.GetPartModel(self)
    return self.data.model
end

--获取宠物名字
function M.GetPartName(self)
    return self.data.name
end


------------------汽车信息-End--------------------------------------

----------------------其它接口--------------------------------

function M.GetGuid(self)
    return  self.guid
 end

function M.GetCarId(self)
   return  self.carId
end


-- 返回cell的绝对坐标
function M.GetAbsolutePosition(self)
    return self.main_inst.AbsolutePosition
end

--获取cell所在的ScreenGui
function M.GetRootGui(self)
    if not  self.RootGui then
        self.RootGui=self.main_inst:FindFirstAncestorOfClass("ScreenGui")
        if not self.RootGui then
            self.RootGui=self.main_inst:FindFirstAncestorOfClass("BillboardGui")
        end
    end
    return self.RootGui
end



return M